﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using Orid.Management.Statistics.Tools.Decomposition.Logic;


namespace Orid.Management.Statistics.Tools.Decomposition.UI
{
    delegate void SetProcessBar(int current,int Count);
    delegate void SetMsgBar(string Msg);
    delegate void showMessage(string msg);
    delegate void normal();


    public partial class FormMain : Form
    {
        private FormTree formTree;
        private FormManageDecompositionOrders fromDecompositionOrders;
        private System.Timers.Timer aTimer = new System.Timers.Timer();
        private bool isRun = false;

        public bool IsRun
        {
            get { return isRun; }
            set 
            { 
                isRun = value;
                if (isRun)
                { 
                    this.toolStripButtonStar.Enabled = true;
                    this.toolStripButtonStar.Image = Properties.Resources.menuRun;
                }
                else
                { 
                    //this.toolStripButtonStar.Enabled = false;
                    this.toolStripButtonStar.Image = Properties.Resources.menuStop;
                }

                Application.DoEvents();
            }
        }

        public FormMain()
        {
            InitializeComponent();
            this.Icon = Properties.Resources.png_0509;
            this.Text =  "DecompositionTools--"+Config.Setting.SettingInstance.ServerName;
        }

         private void FormMain_Load(object sender, EventArgs e)
        {
            formTree = new FormTree();
              formTree.Show(dockPanelMain, DockState.DockLeft);
        
         fromDecompositionOrders = new FormManageDecompositionOrders(this);
         fromDecompositionOrders.Show(dockPanelMain, DockState.Document);

      
         this.toolStripButtonConfig.Click += new EventHandler(toolStripButtonConfig_Click);
         this.toolStripButtonAddDecompositionData.Click += new EventHandler(toolStripButtonAddDecompositionData_Click);
         this.toolStripButtonStar.Click += new EventHandler(toolStripButtonStar_Click);

         #region 设定自动运行时间

         double interval = 60;
         if (Config.Setting.SettingInstance.AutoInterval != null)
         {
             interval = Convert.ToDouble(Config.Setting.SettingInstance.AutoInterval);
         }

         interval = interval * 60 * 1000;

         aTimer.Enabled = false;
         aTimer.Interval =   interval;
         aTimer.Elapsed += new System.Timers.ElapsedEventHandler(aTimer_Elapsed);

        #endregion

        }

          

         void toolStripButtonStar_Click(object sender, EventArgs e)
         {
             if (isRun)
             {  IsRun = false;
                 onStar(e);                
               
             }
             else
             { IsRun = true;
                 onStop(e);
                
 
             }
         }



         void toolStripButtonAddDecompositionData_Click(object sender, EventArgs e)
         {
             ShowAddDecompositionData();
         }

         void configToolStripMenuItem_Click(object sender, EventArgs e)
         {
             ShowConfigPanel();
         }

        void toolStripButtonConfig_Click(object sender, EventArgs e)
        {
            ShowConfigPanel();
        }

        private void ShowConfigPanel()
        {
            new FormConfig().Show(dockPanelMain, DockState.Document);

             
        }
        private void ShowAddDecompositionData()
        {
            if (( fromDecompositionOrders == null)||(fromDecompositionOrders.IsDisposed))
            {
                fromDecompositionOrders = new FormManageDecompositionOrders(this);
            }

            fromDecompositionOrders.Show(dockPanelMain, DockState.Document);

             
            
        }

        
        public event  EventHandler Stop,Star ,AutoRun;

        private void onStar(EventArgs e)
        {

            if (Star != null)
            {
                Star(this, e);
            }          

        }
        private void onStop(EventArgs e)
          {
            if (Stop != null)
            {
                Stop(this,e);
            }
        }

        private void onAutoRun(EventArgs e)
        {
            if (AutoRun != null)
            {
                AutoRun(this, e);
            }
        }


        //public void SetProcessBarValue(int value)
        //{
        //    this.toolStripProgressBarMain.Value = value;
 
        //}
        //public void SetProcessBarMaxValue(int value)
        //{
        //    this.toolStripProgressBarMain.Maximum = value;

        //}


        public void SetProcessBarMain(int current,int Max)
        {
            this.toolStripProgressBarMain.Maximum = Max;
            this.toolStripProgressBarMain.Value = current;
        
            Application.DoEvents();
        }
        //public void SetProcessBarTotal(int current, int Max)
        //{

        //    this.toolStripStatusLabelMsg
        //        .Text = "处理状态:"+ current.ToString() +"/" + Max.ToString();
        //    Application.DoEvents();
        //}

        public void SetMsgBar(string Msg)
        {
            this.toolStripStatusLabelMsg.Text = Msg;
            Application.DoEvents();
        }

        public void showMessage(string Msg)
        {
            if ((formTree != null) &&  (!formTree.IsDisposed) )
            {
                formTree.ShowMessage(Msg);
            }
        }

        private void toolStripButtonMonitor_Click(object sender, EventArgs e)
        {
            if  ((formTree == null) || (formTree.IsDisposed))
            {
                formTree = new FormTree();
             
            }   formTree.Show(dockPanelMain, DockState.DockLeft);
        }

 

        private void toolStripButtonTimer_Click(object sender, EventArgs e)
        {
            if (aTimer.Enabled)
            {
                aTimer.Stop();
                aTimer.Enabled = false;
                toolStripButtonTimer.Image = Properties.Resources.clock;
                toolStripButtonTimer.ToolTipText = "开启自动运行";

            }
            else
            {
                toolStripButtonTimer.Image = Properties.Resources.clock_red;
                toolStripButtonTimer.ToolTipText = "停止自动运行";

                aTimer.Enabled = true;
                aTimer.Start();

            }
        }

        void aTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            onAutoRun(e);
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            AboutBoxDecompositionTools child = new AboutBoxDecompositionTools();
            child.ShowDialog();
        }

        public void SetProcessBarBusy()
        {
            this.toolStripProgressBarMain.Style = ProgressBarStyle.Marquee;
        }

        public void SetProcessBarNormal()
        {
            this.toolStripProgressBarMain.Style = ProgressBarStyle.Continuous;
        }
    }
}
